//
//  MineBenefitsCell.swift
//  SCA
//
//  Created by tjun on 2025/9/23.
//

import UIKit
import SnapKit



struct MineBenefitsModel {
    let date: String
    let prizeDisp: String
    let statusDisp: String
    let status: Int // 1 - 待处理 2 - 已兑换 3 - 已发送
    let Id: Int

    init(dict: [String: Any]) {
        let cdate = dict.stringValue("cdate")
        self.date = formatDate(cdate, dateFormat: "yyyy-MM-dd")
        self.prizeDisp = dict.stringValue("prize_disp")
        self.statusDisp = dict.stringValue("status_disp")
        self.status = dict.intValue("status")
        self.Id = dict.intValue("id")
    }
}

class MineBenefitsView: UIView {
    
    let dateLabel = UILabel(font: UIFontMake(14), textColor: UIColor.white, textAlignment: .left)
    let prizeLabel = UILabel(font: UIFontMake(14), textColor: UIColor.white, textAlignment: .center)
    let stateLabel = UILabel(font: UIFontMake(14), textColor: UIColor.white, textAlignment: .center)
    let operateButton = UIButton(type: .system, font: UIFontMake(14), textColor: UIColor.white)

    override init(frame: CGRect) {
        super.init(frame: frame)

        let contentView = UIView().addTo(self)
        contentView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(12)
            make.top.bottom.equalToSuperview()
        }

        contentView.addSubview(dateLabel)
        dateLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.3)
        }
        
        contentView.addSubview(prizeLabel)
        prizeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(dateLabel.snp.right)
            make.centerY.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.25)
        }
 
        contentView.addSubview(stateLabel)
        stateLabel.snp.makeConstraints { (make) in
            make.left.equalTo(prizeLabel.snp.right)
            make.centerY.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.25)
        }
        
        operateButton.contentHorizontalAlignment = .right
        contentView.addSubview(operateButton)
        operateButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.equalTo(30)
            make.width.equalToSuperview().multipliedBy(0.2)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class MineBenefitsCell: BaseTableViewCell {
    
    var model: MineBenefitsModel! {
        didSet {
            mineBenefitsView.prizeLabel.text = model.prizeDisp
            mineBenefitsView.dateLabel.text = model.date
            mineBenefitsView.stateLabel.text = model.statusDisp
            if model.status == 1 {
                let text = "兑换".localizedString
                 let attrString = NSAttributedString(
                     string: text,
                     attributes: [
                         .underlineStyle: NSUnderlineStyle.single.rawValue,
                         .foregroundColor: UIColor.white // 颜色可以自己调
                     ]
                 )
                mineBenefitsView.operateButton.setAttributedTitle(attrString, for: .normal)
            } else if model.status == 2 {
                mineBenefitsView.operateButton.setAttributedTitle(nil, for: .normal)
                mineBenefitsView.operateButton.setTitle("已兑换".localizedString, for: .normal)
            } else if model.status == 3 {
                mineBenefitsView.operateButton.setAttributedTitle(nil, for: .normal)
                mineBenefitsView.operateButton.setTitle("已发送".localizedString, for: .normal)
            } else {
                mineBenefitsView.operateButton.setAttributedTitle(nil, for: .normal)
                mineBenefitsView.operateButton.setTitle("", for: .normal)
            }
        }
    }
    
    var onOperateButtonTapped: (() -> Void)?
    
    var indexPath: IndexPath? {
        didSet {
            guard let indexPath = indexPath else { return }
            if indexPath.row % 2 == 0 {
                mineBenefitsView.backgroundColor = .clear
            } else {
                mineBenefitsView.backgroundColor = UIColorMake(0x2E3036)
            }
        }
    }

    private let mineBenefitsView = MineBenefitsView()
 
    override func setSubView() {
        super.setSubView()
        
        selectionStyle = .none
        
        backgroundColor = AppStyle.color.background
        
        mineBenefitsView.addCorner(radius: 10)
        mineBenefitsView.operateButton.addTarget(self, action: #selector(operateButtonTapped), for: .touchUpInside)
        mineBenefitsView.backgroundColor = UIColorMake(0x2E3036)
        contentView.addSubview(mineBenefitsView)
        mineBenefitsView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.bottom.equalToSuperview()
        }

    }
    
    @objc func operateButtonTapped() {
        onOperateButtonTapped?()
    }
}


